/*
 * Copyright (C) 2010-2011 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
 * limitations under the License.
  */
package app.timetogo;

import android.content.Context;
import android.content.SharedPreferences;

import android.os.Bundle;

import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceManager;
import android.preference.PreferenceScreen;

import app.timetogo.util.MorseCodeConverter;

public final class Settings extends PreferenceActivity {
    private static final String POLLING_PERIOD = "POLLING_PERIOD";
    private static final String REFRESH_RATE = "REFRESH_RATE";
    private static final String SNOOZE_TIME = "SNOOZE_TIME";
    private static final String DEFAULT_REFRESH_RATE = "20";
    private static final String DEFAULT_POLLING_PERIOD = "15";
    private static final String DEFAULT_SNOOZE_TIME = "10";

    public static int getRefreshRateSeconds(final Context c) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return Integer.parseInt(prefs.getString(REFRESH_RATE, DEFAULT_REFRESH_RATE));
    }

    // Do not enable in production.
    private static boolean DEBUG = false;

    public static int getPollingPeriodMinutes(final Context c) {
        if (DEBUG) {
            return 1;
        }

        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return Integer.parseInt(prefs.getString(POLLING_PERIOD, DEFAULT_POLLING_PERIOD));
    }

    public static int getSnoozeTimeMinutes(final Context c) {
        final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

        return Integer.parseInt(prefs.getString(SNOOZE_TIME, DEFAULT_SNOOZE_TIME));
    }

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setPreferenceScreen(createPreferenceHierarchy());
    }

    private PreferenceScreen createPreferenceHierarchy() {
//		getPreferenceManager().setSharedPreferencesMode();
        // Root
        final PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);

        final PreferenceCategory timingPrefCat = new PreferenceCategory(this);
        timingPrefCat.setTitle(R.string.timing_koto);
        root.addPreference(timingPrefCat);

        final ListPreference pollingPeriodPref = new ListPreference(this);
        pollingPeriodPref.setEntries(R.array.polling_periods);
        pollingPeriodPref.setEntryValues(R.array.polling_periods);
        pollingPeriodPref.setDialogTitle(R.string.polling_period);
        pollingPeriodPref.setKey(POLLING_PERIOD);
        pollingPeriodPref.setTitle(R.string.polling_period);
        pollingPeriodPref.setSummary(R.string.polling_period_desc);
        pollingPeriodPref.setDefaultValue(DEFAULT_POLLING_PERIOD);
        timingPrefCat.addPreference(pollingPeriodPref);

        final ListPreference refreshRatePref = new ListPreference(this);
        refreshRatePref.setEntries(R.array.refresh_rates);
        refreshRatePref.setEntryValues(R.array.refresh_rates);
        refreshRatePref.setDialogTitle(R.string.refresh_rate);
        refreshRatePref.setKey(REFRESH_RATE);
        refreshRatePref.setTitle(R.string.refresh_rate);
        refreshRatePref.setSummary(R.string.refresh_rate_desc);
        refreshRatePref.setDefaultValue(DEFAULT_REFRESH_RATE);
        timingPrefCat.addPreference(refreshRatePref);

        final ListPreference snoozeTimePref = new ListPreference(this);
        snoozeTimePref.setEntries(R.array.snooze_times);
        snoozeTimePref.setEntryValues(R.array.snooze_times);
        snoozeTimePref.setDialogTitle(R.string.snooze_time);
        snoozeTimePref.setKey(SNOOZE_TIME);
        snoozeTimePref.setTitle(R.string.snooze_time);
        snoozeTimePref.setSummary(R.string.snooze_time_desc);
        snoozeTimePref.setDefaultValue(DEFAULT_SNOOZE_TIME);
        timingPrefCat.addPreference(snoozeTimePref);

        return root;
    }

    public static long[] getVibratePattern() {
        return MorseCodeConverter.pattern(" Q N U");
    }

    public static long[] getVibratePatternNothingFound() {
        return MorseCodeConverter.pattern(" S O S");
    }
}
